﻿#include "melsec_3e.h"
#include <QString>
#include <QtDebug>

Melsec_3E::Melsec_3E()
{

}

QString errorCodeString(uint16_t code)
{
    switch (code)
    {
    case 0:
        return u8"正常执行命令";
    case 0xC050:
        return u8"制定的子标头是非数值BINARY代码";
    case 0x4A00:
        return u8"指定的网络编号不是0x00";
    case 0x4B00:
        return u8"指定的PC编号不是0xFF或者指定的IO编号不是0x03FF";
    case 0xC058:
        return u8"执行BINARY代码通信时，指定的数据比设定的软元件数或者块短";
    case 0xC059:
        return u8"制定了不支持的命令或者子命令";
    case 0xC061:
        return u8"以二进制代码进行通讯时，指定的数据比设定的软元件数或者块短";
    case 0xC05B:
        return u8"以二进制代码进行通讯时，制定了不存在的软元件代码";
    case 0xC056:
        return u8"指定的软元件编号超出范围";
    case 0xC051:
        return u8"指定的软元件数超出了范围";
    case 0xC05C:
        return u8"将子命令指定为0001， 执行处理位软元件命令时，却指定了字软元件";
    case 0x4080:
        return u8"子块数和软元件数均指定为 0";
    case 0xC05D:
        return u8"执行监控器登录之前，执行了监控器读取";
    case 0x4043:
        return u8"指定的单元不存在";
    case 0xC05F:
        return u8"在CPU单元中未登录的状态下，试图切换到RUN模式";
//    case 0xC051:
 //       return u8"接收的折返数据个数超出了960";
    }
}

int Melsec_Comm_3E_BINARY::CPUtype(QTcpSocket *socket, uint32_t timeout, QString &cpu_string, uint16_t &cpu_code)
{
    CommandFrame_0101_BINARY frame;
    QByteArray cmd = frame.build();
    socket->write(cmd);
    socket->waitForReadyRead(timeout);
    //接收状态，防止读取的时候出现该状态
    QByteArray data = socket->readAll();

    ResponseFrame_MC_3E_BINARY resp(data);
    uint16_t ret = resp.errorCode();
    if(ret == 0)
    {
        QByteArray text = resp.responseData();
        cpu_string = text.mid(0, 16);
        cpu_string.trimmed();

        QString num(text.mid(16, 2)) ;
        cpu_code = num.toUInt(nullptr, 16);
    }
    return ret;
}

int Melsec_Comm_3E_BINARY::run(QTcpSocket *socket, uint32_t timeout, int mode, int clear_mode)
{
    CommandFrame_1001_BINARY frame;
    QByteArray cmd = frame.build(mode, clear_mode);
    socket->write(cmd);
    socket->waitForReadyRead(timeout);
    //接收状态，防止读取的时候出现该状态
    QByteArray data = socket->readAll();
    qDebug() << data;
    ResponseFrame_MC_3E_BINARY resp(data);
    return resp.errorCode();
}

int Melsec_Comm_3E_BINARY::stop(QTcpSocket *socket, uint32_t timeout, int mode)
{
    CommandFrame_1002_BINARY frame;
    QByteArray cmd = frame.build(mode);
    socket->write(cmd);
    socket->waitForReadyRead(timeout);
    //接收状态，防止读取的时候出现该状态
    QByteArray data = socket->readAll();
    ResponseFrame_MC_3E_BINARY resp(data);
    return resp.errorCode();
}

int Melsec_Comm_3E_BINARY::reset(QTcpSocket *socket, uint32_t timeout, int mode)
{
    CommandFrame_1006_BINARY frame;
    QByteArray cmd = frame.build(mode);
    socket->write(cmd);
    socket->waitForReadyRead(timeout);
    //接收状态，防止读取的时候出现该状态
    QByteArray data = socket->readAll();
    qDebug() << data;
    ResponseFrame_MC_3E_BINARY resp(data);
    return resp.errorCode();
}

int Melsec_Comm_3E_BINARY::pause(QTcpSocket *socket, uint32_t timeout, int mode)
{
    CommandFrame_1003_BINARY frame;
    QByteArray cmd = frame.build(mode);
    socket->write(cmd);
    socket->waitForReadyRead(timeout);
    //接收状态，防止读取的时候出现该状态
    QByteArray data = socket->readAll();

    qDebug() << data;
    ResponseFrame_MC_3E_BINARY resp(data);
    return resp.errorCode();
}

int Melsec_Comm_3E_BINARY::loopback(QTcpSocket *socket, uint32_t timeout, const QByteArray &in_data, QByteArray &out_data)
{
    CommandFrame_0619_BINARY frame;
    QByteArray cmd = frame.build(in_data);
    socket->write(cmd);
    socket->waitForReadyRead(timeout);
    //接收状态，防止读取的时候出现该状态
    QByteArray data = socket->readAll();
    ResponseFrame_MC_3E_BINARY resp(data);
    uint16_t ret = resp.errorCode();
    if(ret) return ret;

    //out_data = resp.responseData().mid(4);
    return ret;
}

int Melsec_Comm_3E_BINARY::write(QTcpSocket *socket, QByteArray data)
{
    int iSnd = 0, iRet = 0;
    int iLength = data.length();
    while (iSnd < iLength)
    {
        iRet = socket->write(data.data() + iSnd, iLength - iSnd);
        if (iRet < 1)
        {
            return -1;
        }
        iSnd += iRet;
    }

    return 0;
}

QByteArray Melsec_Comm_3E_BINARY::read(QTcpSocket *socket, int length)
{
    QByteArray data(length, Qt::Uninitialized);
    int iRev = 0, iRet = 0;
    while (iRev < length)
    {
        iRet = socket->read(data.data() + iRev, length - iRev);
        if (iRet < 1)
        {
            return data.left(0);
        }
        iRev += iRet;
    }
    return data;
}

QBitArray Melsec_Comm_3E_BINARY::readBits(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QBitArray array;
    CommandFrame_0401_BINARY frame;
    QByteArray cmd = frame.readBits(code, addr, N);
    //socket->write(cmd);
    if (write(socket, cmd))
        return array;
    if (!socket->waitForReadyRead(timeout))
        qDebug() << "readBits waitForReadyRead Timeout.";
        //return array;
    //接收状态，防止读取的时候出现该状态
    //QByteArray data = socket->readAll();
    QByteArray head = read(socket, 11);
    if (head.size() == 0)
        return array;
    ResponseFrame_MC_3E_BINARY resp(head);

    if (!resp.checkLegal())
        return  array;

    if (resp.errorCode())
    {
        socket->readAll();
        return array;
    }

    if (resp.dataLength() == 0)
        return array;

    QByteArray data = read(socket, resp.dataLength());
    if(data.size() != N)
        return array;

    array.resize(N);
    for(int i = 0; i < N; i++)
    {
        if( data.at(i) == '1') array.setBit(i);
    }
    return array;
}

QByteArray Melsec_Comm_3E_BINARY::readBytes(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray array;
    CommandFrame_0401_BINARY frame;
    QByteArray cmd = frame.readBytes(code, addr, N);
    //socket->write(cmd);
    if (write(socket, cmd) != 0)
        return array;
    //socket->waitForReadyRead(timeout);
    if (!socket->waitForReadyRead(timeout))
    {
        //return array;
        qDebug() << "readBytes waitForReadyRead Timeout.";
    }
    //接收状态，防止读取的时候出现该状态
    //QByteArray data = socket->readAll();
    QByteArray head = read(socket, 11);
    if (head.size() == 0)
        return array;
    ResponseFrame_MC_3E_BINARY resp(head);
    if (!resp.checkLegal())
        return array;

    if(resp.errorCode())
    {
        socket->readAll();
        return array;
    }

    if (resp.dataLength() == 0)
        return array;

    QByteArray data = read(socket, resp.dataLength());
    //data = resp.responseData();
    if(data.size() != 2 * N)
        return array;

    return data;
}

QVector<int16_t> Melsec_Comm_3E_BINARY::readInt16(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray data = readBytes(socket, timeout, code, addr, N);
    QVector<int16_t> array;
    if (data.size() != 2 * N)
        return array;
    array.resize(N);
    for(int i = 0; i < N; i++)
    {
        uint8_t *i8 = (uint8_t *)&array[i];
        /*i8[0] = data[2 * i];
        i8[1] = data[2 * i + 1];*/
        memcpy(i8, data.data() + 2 * i, 2);
    }

    return array;
}


QVector<int32_t> Melsec_Comm_3E_BINARY::readInt32(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray data = readBytes(socket, timeout, code, addr, 2 * N);
    QVector<int32_t> array;
    if (data.size() != 4 * N)
        return array;
    array.resize(N);
    for(int i = 0; i < N; i++)
    {
        uint8_t *i8 = (uint8_t *)&array[i];
//        i8[0] = data[4 * i + 0];
//        i8[1] = data[4 * i + 1];
//        i8[2] = data[4 * i + 2];
//        i8[3] = data[4 * i + 3];
        memcpy(i8, data.data() + 4 * i, 4);
    }

    return array;
}

QVector<int64_t> Melsec_Comm_3E_BINARY::readInt64(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray data = readBytes(socket, timeout, code, addr, 4 * N);
    QVector<int64_t> array;
    if (data.size() != 8 * N)
        return array;
    array.resize(N);
    for(int i = 0; i < N; i++)
    {
        uint8_t *i8 = (uint8_t *)&array[i];
//        i8[0] = data[8 * i + 0];
//        i8[1] = data[8 * i + 1];
//        i8[2] = data[8 * i + 2];
//        i8[3] = data[8 * i + 3];
//        i8[4] = data[8 * i + 4];
//        i8[5] = data[8 * i + 5];
//        i8[6] = data[8 * i + 6];
//        i8[7] = data[8 * i + 7];
        memcpy(i8, data.data() + 8 * i, 8);
    }
    return array;
}

QVector<float> Melsec_Comm_3E_BINARY::readFloat(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray data = readBytes(socket, timeout, code, addr, 2 * N);
    QVector<float> array;

    if (data.size() != 4 * N)
        return array;
    array.resize(N);
    for(int i = 0; i < N; i++)
    {
        uint8_t *i8 = (uint8_t *)&array[i];
//        i8[0] = data[4 * i + 0];
//        i8[1] = data[4 * i + 1];
//        i8[2] = data[4 * i + 2];
//        i8[3] = data[4 * i + 3];
        memcpy(i8, data.data() + 4 * i, 4);
    }
    return array;
}


QVector<double> Melsec_Comm_3E_BINARY::readDouble(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray data = readBytes(socket, timeout, code, addr, 4 * N);
    QVector<double> array;

    if (data.size() != 8 * N)
        return array;
    array.resize(N);
    for(int i = 0; i < N; i++)
    {
        uint8_t *i8 = (uint8_t *)&array[i];
//        i8[0] = data[8 * i + 0];
//        i8[1] = data[8 * i + 1];
//        i8[2] = data[8 * i + 2];
//        i8[3] = data[8 * i + 3];
//        i8[4] = data[8 * i + 4];
//        i8[5] = data[8 * i + 5];
//        i8[6] = data[8 * i + 6];
//        i8[7] = data[8 * i + 7];
        memcpy(i8, data.data() + 8 * i, 8);
    }
    return array;
}

QByteArray Melsec_Comm_3E_BINARY::readString(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray data = readBytes(socket, timeout, code, addr, N);
    QByteArray strData;
    if (data.size() != 2 * N)
        return strData;

    return data;
     //return QString(data);
}

uint16_t Melsec_Comm_3E_BINARY::writeBits(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, QBitArray array)
{
    CommandFrame_1401_BINARY frame;
    QByteArray cmd = frame.build(code, addr, array);
    //socket->write(cmd);
    if (write(socket, cmd) != 0)
        return -1;
    if (!socket->waitForReadyRead(timeout))
        qDebug() << "writeBits waitForReadyRead Timeout.";
        //return -1;
    //QByteArray data = socket->readAll();
    QByteArray head = read(socket, 11);
    if (head.size() == 0)
        return -1;
    ResponseFrame_MC_3E_BINARY resp(head);
    if (!resp.checkLegal())
        return -1;

    uint16_t msglen = resp.dataLength();
    if (msglen > 0)
        read(socket, msglen);

    return resp.errorCode();
}

uint16_t Melsec_Comm_3E_BINARY::writeBytes(QTcpSocket *socket, uint32_t timeout, uint8_t code, uint32_t addr, QVector<uint16_t> array)
{
    CommandFrame_1401_BINARY frame;
    QByteArray cmd = frame.build(code, addr, array);
    //socket->write(cmd);
    if (write(socket, cmd) != 0)
        return -1;
    //socket->waitForReadyRead(timeout);
    if (!socket->waitForReadyRead(timeout))
    {
        //return -1;
        qDebug() << "writeBytes waitForReadyRead Timeout.";
    }
    //QByteArray data = socket->readAll();
    QByteArray head = read(socket, 11);
    if (head.size() == 0)
        return -1;
    ResponseFrame_MC_3E_BINARY resp(head);
    if (!resp.checkLegal())
        return -1;

    uint16_t msglen = resp.dataLength();
    if (msglen > 0)
        read(socket, msglen);

     return resp.errorCode();
}

ResponseFrame_MC_3E_BINARY::ResponseFrame_MC_3E_BINARY(QByteArray frame)
{
    m_data = frame;
}

bool ResponseFrame_MC_3E_BINARY::parser(QByteArray frame)
{
    m_data = frame;
    return false;
}
bool ResponseFrame_MC_3E_BINARY::checkLegal()
{
    if (m_data.length() >= 11)
    {
        uint8_t d0 = m_data[0];
        uint8_t d1 = m_data[1];
        uint8_t d3 = m_data[3];
        uint8_t d4 = m_data[4];
        uint8_t d5 = m_data[5];

        if (d0 == 0xD0 && d1 == 0x00 && d3 == 0xFF
                && d4 == 0xFF && d5 == 0x03)
        {
            return true;
        }
    }
    else
        return false;
}
uint16_t ResponseFrame_MC_3E_BINARY::dataLength()
{
    //return m_data[10] << 8 | m_data[9];
    uint16_t u16;

    memcpy((uint8_t *)&u16, m_data.data() + 7, 2);

    return u16 - 2;
}

uint16_t ResponseFrame_MC_3E_BINARY::errorCode()
{
    //return m_data[10] << 8 | m_data[9];
    uint16_t u16;

    memcpy((uint8_t *)&u16, m_data.data() + 9, 2);

    return u16;
}

QByteArray ResponseFrame_MC_3E_BINARY::responseData()
{
    return m_data.mid(11);
}

QByteArray ResponseFrame_MC_3E_BINARY::errorData()
{
    return m_data.mid(11);
}

QByteArray ErrorData_MC_3E_BINARY::netNumber()
{
    return m_data.mid(0, 1);
}

QByteArray ErrorData_MC_3E_BINARY::PLCNumber()
{
    return m_data.mid(1, 1);
}

QByteArray ErrorData_MC_3E_BINARY::IONumber()
{
    return m_data.mid(2, 2);
}

QByteArray ErrorData_MC_3E_BINARY::command()
{
    return m_data.mid(5, 2);
}

QByteArray ErrorData_MC_3E_BINARY::subCommand()
{
    return m_data.mid(7, 2);
}

QByteArray CommandFrame_1401_BINARY::build(uint8_t code, uint32_t addr, QBitArray array)
{
    QByteArray data;
    int length = array.size();
    data.resize(12 + length / 2 + length % 2);
    data[0] = 0x10;
    data[1] = 0x00;
    data[2] = 0x01;
    data[3] = 0x14;
    data[4] = 0x01;
    data[5] = 0x00;
    data[6] = (uint8_t)((addr >> 0) & 0xff);
    data[7] = (uint8_t)((addr >> 8) & 0xff);
    data[8] = (uint8_t)((addr >> 16) & 0xff);
    data[9] = code;
    data[10] = length % 256;
    data[11] = length / 256;

    uint8_t y = 0;
    for(uint16_t i = 0; i < length; i++)
    {
        uint8_t x = array[i] ? 1 : 0;

        if (i % 2 == 0)
             y = x << 4;
        else
             y |= x;
        data[12 + i / 2] = y;
    }

    return buildFrame(data);
}

QByteArray CommandFrame_1401_BINARY::build(uint8_t code, uint32_t addr, QVector<uint16_t> array)
{
    int length = array.size();
    QByteArray data(12 + length *2, Qt::Uninitialized);
    data[0] = 0x10;
    data[1] = 0x00;
    data[2] = 0x01;
    data[3] = 0x14;
    data[4] = 0x00;
    data[5] = 0x00;
    data[6] = (uint8_t)((addr >> 0) & 0xff);
    data[7] = (uint8_t)((addr >> 8) & 0xff);
    data[8] = (uint8_t)((addr >> 16) & 0xff);
    data[9] = code;
    data[10] = length % 256;
    data[11] = length / 256;

    for(uint16_t i = 0; i < length; i++)
    {
        uint16_t x = array.at(i);
        data[12 + 2 * i] = x & 0xff;
        data[12 + 2 * i + 1] = (x >> 8) & 0xff;
    }
    return buildFrame(data);
}

QByteArray CommandFrame_1401_BINARY::bitToArray(QBitArray array)
{
    QByteArray data;
    int N = array.size();
    for(int i = 0; i < N; i++)
    {
        if(array.at(i))
        {
            data.push_back('1');
        }
        else
        {
            data.push_back('0');
        }
    }
    return data;
}


QByteArray CommandFrame_MC_3E_BINARY::buildFrame(QByteArray cmddata)
{
    QByteArray data(9, Qt::Uninitialized);

    data[0] = 0x50;
    data[1] = 0x00;
    data[2] = 0x00;
    data[3] = 0xFF;
    data[4] = 0xFF;
    data[5] = 0x03;
    data[6] = 0x00;
    data[7] = (uint8_t)(cmddata.length() % 256);
    data[8] = (uint8_t)(cmddata.length() / 256);

    data.append(cmddata);
    return data;
}

QByteArray CommandFrame_MC_3E_BINARY::textLength(QByteArray text)
{
    uint16_t N = text.length();
    return uint16ToHex(N);
}

QByteArray CommandFrame_MC_3E_BINARY::address(uint32_t addr)
{
    QByteArray bytebuff(3, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((addr >> 0) & 0xff);
    bytebuff[1] = (uint8_t)((addr >> 8) & 0xff);
    bytebuff[2] = (uint8_t)((addr >> 16) & 0xff);

    return bytebuff;
}

QByteArray CommandFrame_MC_3E_BINARY::uint8ToHex(uint8_t x)
{
    QByteArray bytebuff(1, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((x >> 0) & 0xff);

    return bytebuff;
}

QByteArray CommandFrame_MC_3E_BINARY::uint16ToHex(uint16_t x)
{
    QByteArray bytebuff(2, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((x >> 0) & 0xff);
    bytebuff[1] = (uint8_t)((x >> 8) & 0xff);

    return bytebuff;
}

QByteArray CommandFrame_MC_3E_BINARY::uint24ToHex(uint32_t x)
{
    QByteArray bytebuff(3, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((x >> 0) & 0xff);
    bytebuff[1] = (uint8_t)((x >> 8) & 0xff);
    bytebuff[2] = (uint8_t)((x >> 16) & 0xff);

    return bytebuff;
}

QByteArray CommandFrame_MC_3E_BINARY::uint32ToHex(uint32_t x)
{
    QByteArray bytebuff(4, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((x >> 0) & 0xff);
    bytebuff[1] = (uint8_t)((x >> 8) & 0xff);
    bytebuff[2] = (uint8_t)((x >> 16) & 0xff);
    bytebuff[3] = (uint8_t)((x >> 24) & 0xff);

    return bytebuff;
}

QByteArray CommandFrame_MC_3E_BINARY::uint48ToHex(uint64_t x)
{
    QByteArray bytebuff(6, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((x >> 0) & 0xff);
    bytebuff[1] = (uint8_t)((x >> 8) & 0xff);
    bytebuff[2] = (uint8_t)((x >> 16) & 0xff);
    bytebuff[3] = (uint8_t)((x >> 24) & 0xff);
    bytebuff[4] = (uint8_t)((x >> 32) & 0xff);
    bytebuff[5] = (uint8_t)((x >> 40) & 0xff);
    return bytebuff;
}

QByteArray CommandFrame_MC_3E_BINARY::uint64ToHex(uint64_t x)
{
    QByteArray bytebuff(8, Qt::Uninitialized);

    bytebuff[0] = (uint8_t)((x >> 0) & 0xff);
    bytebuff[1] = (uint8_t)((x >> 8) & 0xff);
    bytebuff[2] = (uint8_t)((x >> 16) & 0xff);
    bytebuff[3] = (uint8_t)((x >> 24) & 0xff);
    bytebuff[4] = (uint8_t)((x >> 32) & 0xff);
    bytebuff[5] = (uint8_t)((x >> 40) & 0xff);
    bytebuff[6] = (uint8_t)((x >> 48) & 0xff);
    bytebuff[7] = (uint8_t)((x >> 56) & 0xff);
    return bytebuff;
}

QByteArray CommandFrame_1001_BINARY::build(int mode, int clear_mode)
{
    QByteArray cmdtext(10, Qt::Uninitialized);

    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x01;
    cmdtext[3] = 0x10;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令
    if(mode == 1)
    {
        cmdtext[6] = 0x01;
        cmdtext[7] = 0x00;//不强制执行
    }
    else
    {
        cmdtext[6] = 0x03;
        cmdtext[7] = 0x00;//强制执行
    }
    switch (clear_mode)
    {
    case 0:
        cmdtext[8] = 0x00; //清空模式,不清空软元件存储器
        break;
    case 1:
        cmdtext[8] = 0x01; //清空模式,清空锁存范围外的软元件存储器
        break;
    case 2:
        cmdtext[8] = 0x02; //清空模式,清空包括锁存范围在内的全部软元件存储器
        break;
    default:
        cmdtext[8] = 0x00; //清空模式
        break;
    }
    cmdtext[9] = 0x00;

    return buildFrame(cmdtext);
}

QByteArray CommandFrame_1002_BINARY::build(int mode)
{
    QByteArray cmdtext(8, Qt::Uninitialized);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x02;
    cmdtext[3] = 0x10;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令
    if(mode == 1)
    {
        cmdtext[6] = 0x01;
        cmdtext[7] = 0x00;//不强制执行
    }
    else
    {
        cmdtext[6] = 0x03;
        cmdtext[7] = 0x00;//强制执行
    }
    return buildFrame(cmdtext);
}

QByteArray CommandFrame_1003_BINARY::build(int mode)
{
    QByteArray cmdtext(8, Qt::Uninitialized);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x03;
    cmdtext[3] = 0x10;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令
    if(mode == 1)
    {
        cmdtext[6] = 0x01;
        cmdtext[7] = 0x00;//不强制执行
    }
    else
    {
        cmdtext[6] = 0x03;
        cmdtext[7] = 0x00;//强制执行
    }
    return buildFrame(cmdtext);
}

QByteArray CommandFrame_1006_BINARY::build(int mode)
{
    QByteArray cmdtext(8, Qt::Uninitialized);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x06;
    cmdtext[3] = 0x10;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令
    if(mode == 1)
    {
        cmdtext[6] = 0x01;
        cmdtext[7] = 0x00;//不强制执行
    }
    else
    {
        cmdtext[6] = 0x03;
        cmdtext[7] = 0x00;//强制执行
    }
    return buildFrame(cmdtext);
}

QByteArray CommandFrame_1005_BINARY::build(int mode)
{
    QByteArray cmdtext(8, Qt::Uninitialized);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x05;
    cmdtext[3] = 0x10;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令
    if(mode == 1)
    {
        cmdtext[6] = 0x01;
        cmdtext[7] = 0x00;//不强制执行
    }
    else
    {
        cmdtext[6] = 0x03;
        cmdtext[7] = 0x00;//强制执行
    }
    return buildFrame(cmdtext);
}

QByteArray CommandFrame_0101_BINARY::build()
{
    QByteArray cmdtext(6, Qt::Uninitialized);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x01;
    cmdtext[3] = 0x01;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令

    return buildFrame(cmdtext);
}

QByteArray CommandFrame_0619_BINARY::build(QByteArray t)
{
    QByteArray cmdtext(6 + t.length(), Qt::Uninitialized);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;// CPU 监视定时器
    cmdtext[2] = 0x19;
    cmdtext[3] = 0x06;// 命令
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;// 子命令
    cmdtext[6] = (uint8_t)(t.length() % 256);
    cmdtext[7] = (uint8_t)(t.length() / 256);

    cmdtext.append(t);
    return buildFrame(cmdtext);
}

QByteArray CommandFrame_0401_BINARY::readBits(uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray cmdtext;
    cmdtext.resize(12);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;
    cmdtext[2] = 0x01;
    cmdtext[3] = 0x04;
    cmdtext[4] = 0x01;
    cmdtext[5] = 0x00;
    cmdtext[6] = (uint8_t)((addr >> 0) & 0xff);
    cmdtext[7] = (uint8_t)((addr >> 8) & 0xff);
    cmdtext[8] = (uint8_t)((addr >> 16) & 0xff);
    cmdtext[9] = code;
    cmdtext[10] = (uint8_t)(N % 256);
    cmdtext[11] = (uint8_t)(N / 256);

    return buildFrame(cmdtext);
}

QByteArray CommandFrame_0401_BINARY::readBytes(uint8_t code, uint32_t addr, uint16_t N)
{
    QByteArray cmdtext;
    cmdtext.resize(12);
    cmdtext[0] = 0x10;
    cmdtext[1] = 0x00;
    cmdtext[2] = 0x01;
    cmdtext[3] = 0x04;
    cmdtext[4] = 0x00;
    cmdtext[5] = 0x00;
    cmdtext[6] = (uint8_t)((addr >> 0) & 0xff);
    cmdtext[7] = (uint8_t)((addr >> 8) & 0xff);
    cmdtext[8] = (uint8_t)((addr >> 16) & 0xff);
    cmdtext[9] = code;
    cmdtext[10] = (uint8_t)(N % 256);
    cmdtext[11] = (uint8_t)(N / 256);

    return buildFrame(cmdtext);
}
